package com.woniu.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniu.user.controller.form.LessonAddForm;
import com.woniu.user.exception.WebException;
import com.woniu.user.exception.WebExceptionCode;
import com.woniu.user.model.Coach;
import com.woniu.user.model.Lesson;
import com.woniu.user.dao.LessonDao;
import com.woniu.user.service.CoachService;
import com.woniu.user.service.LessonService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.user.util.BeanCopyUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 课程信息表 服务实现类
 * </p>
 *
 * @author Silas
 * @since 2023-02-15
 */
@Service
public class LessonServiceImpl extends ServiceImpl<LessonDao, Lesson> implements LessonService {
    @Resource
    private LessonDao lessonDao;
    @Resource
    private CoachService coachService;

    @Override
    public List<Lesson> findMyLesson(Integer coachId) {
        List<Lesson> lessons = lessonDao.selectList(new QueryWrapper<Lesson>().eq("coach_id", coachId).eq("state", "上架"));
        return lessons;
    }

    @Override
    public void add(LessonAddForm lessonAddForm, Integer userId) {
        Coach coach = coachService.getOne(new QueryWrapper<Coach>().eq("account_id", userId));
        if (coach.getPrice() == null || "".equals(coach.getPrice()))
            throw new WebException(WebExceptionCode.YOU_DON_NOT_SET_PRICE);
        Lesson lesson = new Lesson();
        lesson.setCoachId(coach.getCoachId());
        lesson.setType(lessonAddForm.getType());
        lesson.setName(lessonAddForm.getName());
        lesson.setCoachId(coach.getCoachId());
        lesson.setCoachName(coach.getRealname());
        lesson.setVenueId(coach.getVenueId());
        lesson.setVenueName(coach.getVenueName());
        lesson.setPrice(coach.getPrice());
        lesson.setIntroduce(lessonAddForm.getIntroduce());
        lesson.setState("上架");
        save(lesson);
    }

    @Override
    public List<Lesson> getMy(Integer userId) {
        Integer coachId = coachService.getOne(new QueryWrapper<Coach>().eq("account_id", userId)).getCoachId();
        List<Lesson> lessons = lessonDao.selectList(new QueryWrapper<Lesson>().eq("coach_id", coachId));
        return lessons;
    }
}
